package com.javatech.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * the JsonUtil
 *
 * @author java-tech
 * @date 2025-03-06
 */
public final class JsonUtil {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    static {
        OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
    }

    public static String toStr(Object obj) {
        try {
            return OBJECT_MAPPER.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static <T> T toJson(String str, Class<T> valueType) {
        try {
            return OBJECT_MAPPER.readValue(str, valueType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
